package com.leyou.item.controller;

import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/spec")
public class SpecController {

    @Autowired
    private SpecGroupService specGroupService;

    @Autowired
    private SpecParamService specParamService;


    //根据商品分类ID查询规格组的集合

    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> querycategoryById(@RequestParam("id")Long cid){
        return ResponseEntity.ok(this.specGroupService.querycategoryById(cid));
    }

    //根据分类id查询规格组及组内参数
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> querygrouporparam(@RequestParam("id") Long ids){
        return ResponseEntity.ok(this.specGroupService.querygrouporparam(ids));
    }

    //根据条件查询规格参数集合
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> queryparamlist(
            @RequestParam(value = "groupid", required = false)Long gid,
            @RequestParam(value = "searching",required = false)Boolean search,
            @RequestParam(value = " categoryId",required = false)Long cid
    ){
        return ResponseEntity.ok(this.specParamService.queryparam(gid,cid));
    }

    //新增规格组
    @PostMapping("/group")
    public ResponseEntity<SpecGroup> addgroup(@RequestBody SpecGroupDTO specGroupDTO){
        this.specGroupService.addgroup(specGroupDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    //修改规格组
    @PutMapping("/group")
    public ResponseEntity<SpecGroup> updategroup(@RequestBody SpecGroupDTO specGroupDTO){
        this.specGroupService.updategroup(specGroupDTO);
        return ResponseEntity.ok().build();
    }
    //新增规格参数
    @PostMapping("/param")
    public ResponseEntity<SpecParam> addparam(@RequestBody SpecParamDTO specParamDTO){
        this.specParamService.addparam(specParamDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    //修改规格参数
    @PutMapping("/param")
    public ResponseEntity<SpecParam> updateparam(@RequestBody SpecParamDTO specParamDTO){
        this.specParamService.updateparam(specParamDTO);
        return ResponseEntity.ok().build();
    }

    //根据categoryid查询规格组及组内参数
    @GetMapping("/list/of/category")
    public ResponseEntity<List<SpecGroupDTO>> querySpecList(@RequestParam("id") long cid){
        return ResponseEntity.ok(this.specGroupService.querySpecList(cid));
    }
}

